package com.mp.approvalreq.util;

import com.alibaba.fastjson.JSONObject;
import com.sun.mail.util.MailSSLSocketFactory;
import org.apache.commons.lang3.StringUtils;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.Message.RecipientType;
import javax.mail.internet.*;
import java.io.File;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xq
 */
public class EmailUtils {

	private static final String CODER_EMAIL = "497019820@qq.com";

	private static final String CODER_PASSWORD = "jpwqkiugiyrebigb";

	public static void sendErrorMail(String title, String content) {
		try {
			final Properties props = new Properties();
			// 表示SMTP发送邮件，需要进行身份验证
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.host", "smtp.qq.com");
			// 发件人的账号
			props.put("mail.user", CODER_EMAIL);
			// 访问SMTP服务时需要提供的密码
			props.put("mail.password", CODER_PASSWORD);
			MailSSLSocketFactory sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);
			props.put("mail.smtp.ssl.enable", "true");
			props.put("mail.smtp.ssl.socketFactory", sf);

			Authenticator authenticator = new Authenticator() {
				@Override
				protected PasswordAuthentication getPasswordAuthentication() {
					// 用户名、密码
					String userName = props.getProperty("mail.user");
					String password = props.getProperty("mail.password");
					return new PasswordAuthentication(userName, password);
				}
			};
			// 使用环境属性和授权信息，创建邮件会话
			Session mailSession = Session.getInstance(props, authenticator);
			// 创建邮件消息
			MimeMessage message = new MimeMessage(mailSession);
			// 设置发件人
			InternetAddress from = new InternetAddress(props.getProperty("mail.user"));
			from.setPersonal("大管加错误报告");
			message.setFrom(from);

			// 设置收件人
			InternetAddress to = new InternetAddress(CODER_EMAIL);
			message.setRecipient(RecipientType.TO, to);

			// 设置邮件标题
			message.setSubject(title, "UTF-8");


			Multipart multipart = new MimeMultipart();
			BodyPart bodypart = new MimeBodyPart();
			String sign = "<br><div><div style=\" color: rgb(144, 144, 144) ; ;; font-size: 12px; \">------------------</div><div><div><br></div><div><img src=\"https://exmail.qq.com/cgi-bin/viewfile?type=signature&amp;picid=ZX0503-c0ATMRglGRUc8m43r%7E00b7h&amp;uin=2618042675\"></div><div><div id=\"signPreview\" style=\"height: auto; min-height: 80px; padding: 5px;\"><div><span style=\"color: rgb(51, 51, 51); line-height: 1.5;\"><span style=\"font-size: 19px;\"><font face=\"黑体\"><br></font></span></span></div><div><span style=\"color: rgb(51, 51, 51); line-height: 1.5;\"><span style=\"font-size: 19px;\"><font face=\"黑体\">潘芊一 </font>Cherry </span>&nbsp;<font size=\"2\">总裁助理</font></span></div><div style=\"line-height: normal; color: rgb(51, 51, 51); font-size: 12px;\"><font color=\"#999999\"><br></font></div><div style=\"line-height: normal;\"><font color=\"#333333\"><font size=\"1\" face=\"幼圆\">手 机：</font><font size=\"1\">18682251800</font></font></div><div style=\"line-height: normal;\"><font color=\"#333333\" size=\"1\" face=\"幼圆\">微 信：cherrypan_915</font></div><div style=\"line-height: normal;\"><font color=\"#333333\"><font size=\"1\" face=\"幼圆\">邮 箱：</font><font size=\"1\"><a href=\"mailto:cherry@managerplus.cn\" target=\"_blank\">cherry@manag<wbr>erplus.cn</a></font></font></div><div style=\"line-height: normal; color: rgb(51, 51, 51);\"><font size=\"2\"><font color=\"#999999\"><br></font></font></div><div style=\"line-height: normal;\"><font size=\"2\" face=\"幼圆\" color=\"#333333\"><font>深圳</font><span style=\"line-height: 1.5;\">大管加软件与技术服务有限公司</span></font></div><div style=\"line-height: normal;\"><div style=\"font-size: 13px;\"><font face=\"幼圆\" color=\"#333333\">深圳市福田区京基滨河时代大厦A栋9层</font><span style=\"font-size: 14px;\">&nbsp;</span></div></div></div></div></div></div>";
			if (StringUtils.isNotEmpty(content)) {
				content = content.replaceAll("\n", "<br>");
			}
			bodypart.setContent(content + sign, "text/html;charset=utf-8");
			bodypart.setContent(content, "text/html;charset=utf-8");
			multipart.addBodyPart(bodypart);


			// 设置邮件的内容体
			message.setContent(multipart);

			// 发送邮件
			Transport.send(message);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	public static void sendMail(String title, Object obj) {
		if (obj instanceof String) {
			sendErrorMail(title, (String) obj);
		} else {
			sendErrorMail(title, JSONObject.toJSONString(obj));
		}
	}

}
